import 'package:flutter/material.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:myflutter/ws/NamePage.dart';
import 'package:myflutter/ws/gamePage.dart';
import 'package:myflutter/ws/save/SharedPreferencesHelper.dart';
import 'package:myflutter/ws/theme/MyColors.dart';

class PageManager extends StatefulWidget {
  @override
  _PageManagerState createState() => _PageManagerState();
}

class _PageManagerState extends State<PageManager> {
  bool isNamed = false;

  void initialization() async {
    // This is where you can initialize the resources needed by your app while
    // the splash screen is displayed.  Remove the following example because
    // delaying the user experience is a bad design practice!
    // ignore_for_file: avoid_print

    await Future.delayed(const Duration(milliseconds: 500));
    print('go!');
    FlutterNativeSplash.remove();
  }

  @override
  void initState() {
    super.initState();
    _checkNameStatus();
    initialization();
  }

  Future<void> _checkNameStatus() async {
    String name = await SharedPreferencesHelper.getString("name");
    setState(() {
      isNamed = name.isNotEmpty;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      darkTheme: ThemeData.dark(), // 默认的深色主题
      themeMode: ThemeMode.system, // 根据系统的亮暗模式切换主题
      debugShowCheckedModeBanner: false,  // 设置为 false 隐藏 DEBUG 标志
      theme: ThemeData(
        appBarTheme: AppBarTheme(
          color: MyColors.primaryColor,
        ),
      ),
      home: isNamed ? GamePage() : NamePage(),
      routes: {
        '/game': (context) => GamePage(),
        '/name': (context) => NamePage(),
      },
    );
  }
}
